package com.hedl.studyandjava8.java8_3unit.java8_40;

import com.hedl.studyandjava8.java8_3unit.lambdaService.BufferedReaderProcessor;
import com.hedl.studyandjava8.java8_3unit.lambdaService.LambdaNot;
import com.hedl.studyandjava8.java8_3unit.lambdaService.LambdaOne;
import com.hedl.studyandjava8.java8_3unit.lambdaService.LambdaTwo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/19/20:09
 */
public class LambdaMain {

    private final static String SOURCEPATH = "/Users/a101/hello.txt";

    public static String processFile(String address,BufferedReaderProcessor processor){
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(address))){
            //线程不安全的，StringBuilder 通常被推荐用于单线程下进行大量的字符操作，因为它提供了更好的性能
            //StringBuilder builder = new StringBuilder();
            //在多线程环境下，线程安全，StringBuffer 可能是更好的选择，尽管它牺牲了一些性能，但提供了更高的数据一致性和安全性。
            StringBuffer buffer = new StringBuffer();

            String str;
            while((str = processor.process(bufferedReader)) != null){
                buffer.append(str).append("\n");
            }
            return buffer.toString();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        //lambda的基础写法
        LambdaOne<String> test1 = (String str) -> str;
        System.out.println(test1.test("Hello 大西瓜"));

        //LambdaNot<String> test2 = () -> {return "晚上好呀，你在做什么呢！";};
        LambdaNot<String> test2 = () -> "晚上好呀，你在做什么呢！";
        System.out.println(test2.test());

        LambdaTwo<Integer> test3 = (Integer i1,Integer i2) -> i1 + i2;
        System.out.println("等于： " + test3.test(3,3));

        System.out.println("************************************************************************************");

        String result = processFile(SOURCEPATH, (BufferedReader read) -> read.readLine());
        System.out.println(result);
    }
}
